package com.example.zlw.singpixdemo;

import android.app.ActivityManager;
import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.List;

/**
 * Created by ZengLingWen on 2017/6/14.
 */

public class JobHandlerService extends JobService {

    private int jobId = 0x0008;

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        scheduleJob(getJobInfo());
        return Service.START_STICKY;

    }

    private void scheduleJob(JobInfo jobInfo) {
        JobScheduler js = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        js.schedule(jobInfo);
    }

    private JobInfo getJobInfo() {
        /**
         * // jobId每个Job任务的id
         int jobId = 1;
         // 指定你需要执行的JobService
         ComponentName name = new ComponentName(getPackageName(), MyJobService.class.getName()));

         JobInfo.Builder builder = new JobInfo.Bulider(jobId, name);

         builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE); //设置需要的网络条件，默认NETWORK_TYPE_NONE

         builder.setPeriodic(3000);//设置间隔时间

         builder.setMinimumLatency(3000);// 设置任务运行最少延迟时间

         builder.setOverrideDeadline(50000);// 设置deadline，若到期还没有达到规定的条件则会开始执行

         builder.setRequiresCharging(true);// 设置是否充电的条件,默认false

         builder.setRequiresDeviceIdle(false);// 设置手机是否空闲的条件,默认false

         builder.setPersisted(true);//设备重启之后你的任务是否还要继续执行

         JobInfo info = builder.build();
         */
        JobInfo.Builder builder = new JobInfo.Builder(jobId, new ComponentName(this, JobHandlerService.class));
        builder.setPersisted(true);
        builder.setPeriodic(100);
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setRequiresCharging(false);
        builder.setRequiresDeviceIdle(false);
        return builder.build();
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        boolean isRemoteServiceWorking = isServiceWork(this, "com.example.zlw.singpixdemo.MyService");
        if (!isRemoteServiceWorking) {
            //必然会走
            startService(new Intent(this, MyService.class));
        }

        return true;
    }

    private boolean isServiceWork(Context context, String s) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServices =
                activityManager.getRunningServices(128);
        if (runningServices.size() < 0) {
            return false;
        }

        for (ActivityManager.RunningServiceInfo serviceInfo :
                runningServices) {
            String className = serviceInfo.service.getClassName();
            if (className.equals(s)) {
                return true;

            }
        }

        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.e("我是曾令文", "重启JobHandlerService");
        //上面返回true 会走
        scheduleJob(getJobInfo());
        return false;
    }
}
